import os

from django.conf import settings
from rest_framework import serializers

# 申请入团用的序列化类
from apps.club_system.club.models import Club, ClubApplication
from apps.common.account.models import Account


class ClubApplicationDetailSerializer(serializers.ModelSerializer):
    created_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)
    updated_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)

    club = serializers.StringRelatedField(read_only=True)
    club_id = serializers.IntegerField()
    account = serializers.HiddenField(  # 默认为当前创建者
        default=serializers.CurrentUserDefault()
    )

    first_check_status = serializers.CharField(read_only=True)
    second_check_status = serializers.CharField(read_only=True)

    def validate(self, attrs):
        if Club.objects.filter(id=attrs['club_id']).exists() is False:
            raise serializers.ValidationError(detail="社团不存在")
        return attrs

    def update(self, instance, validated_data):
        # 每次修改都将两个审核改为待审核
        validated_data['first_check_status'] = '待审核'
        validated_data['second_check_status'] = '待审核'
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        return instance

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        # 返回个人信息
        ret['realname'] = instance.account.realname
        ret['username'] = instance.account.username
        ret['student_community'] = instance.account.student_community
        ret['politics_status'] = instance.account.politics_status
        ret['faculty_title'] = instance.account.faculty_title
        # 添加文件
        ret['files'] = [{'url': f"{settings.IMAGE_URL}{file.get('data')}", 'name': file.get('file_name')}
                        for file in instance.file]

        return ret

    class Meta:
        model = ClubApplication
        fields = '__all__'


#  社团入团审核序列化器
class ClubApplicationSerializer(serializers.ModelSerializer):
    created_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)
    updated_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)
    club_id = serializers.IntegerField()
    club = serializers.StringRelatedField()

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        # 返回个人信息
        ret['realname'] = instance.account.realname
        ret['username'] = instance.account.username
        ret['student_community'] = instance.account.student_community
        ret['politics_status'] = instance.account.politics_status
        ret['faculty_title'] = instance.account.faculty_title
        ret['club_name'] = instance.club.name  # 添加社团名字
        # 添加文件
        ret['files'] = [{'url': f"{settings.IMAGE_URL}{file.get('data')}", 'name': file.get('file_name')}
                        for file in instance.file]
        return ret

    class Meta:
        model = ClubApplication
        fields = "__all__"
